﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Security;

namespace Smart.UI.BaseClasses
{
    public class BasePage : Page
    {
        public string CurrentIP
        {
            get
            {
                return Request.UserHostAddress;
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                if (Session["__isAuthenticated"] != null)
                {
                    return (bool)Session["__isAuthenticated"];
                }
                return false;
            }
            set
            {
                Session["__isAuthenticated"] = value;
            }
        }

        public int CurrentUserId
        {
            get
            {
                if (Session["__currentUserId"] != null)
                {
                    return (int)Session["__currentUserId"];
                }
                return 0;
            }
            set
            {
                Session["__currentUserId"] = value;
            }
        }

        public void Login(int userId, string displayName, string returnUrl = null)
        {
            FormsAuthentication.SetAuthCookie(displayName, false);
            IsAuthenticated = true;
            CurrentUserId = userId;

            if (returnUrl != null)
            {
                if (!returnUrl.StartsWith("~"))
                {
                    returnUrl = "~" + returnUrl;
                }

                Response.Redirect(returnUrl);
            }
            else
            {
                Response.Redirect("~/Pages/Home.aspx");
            }
        }
    }
}
